package com.example.fairplay.newfpregistration.camera;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;


/**
 * Created by Administrator on 2018/1/24.
 */

public class FaceView extends android.support.v7.widget.AppCompatImageView {
    private Context mContext;
    private Paint mLinePaint;
    private FaceResult faceResult;


    public FaceView(Context context, AttributeSet attrs) {
        super(context,attrs);
        initPaint();
        mContext = context;
    }

    public void setFaces(FaceResult faceResult) {
        this.faceResult = faceResult;
        invalidate();
    }

    public void clearFaces() {
        faceResult = null;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (faceResult == null){
            return;
        }
        canvas.save();
        RectF rectF = new RectF(faceResult.getLeft(), faceResult.getTop()
                , faceResult.getRight(), faceResult.getBottom());
        float l= rectF.left;
        float t = rectF.top;
        // 左上-
        canvas.drawLine(l, t, l + rectF.width() / 6, t, mLinePaint);
        // 右上-
        canvas.drawLine((5 * rectF.width() / 6) + l, t, rectF.width() + l, t,
                mLinePaint);

        // 左上|
        canvas.drawLine(l, t, l, t + (rectF.height() / 6), mLinePaint);
        // 左下|
        canvas.drawLine(l, (5 * rectF.height() / 6) + t, l, t + rectF.height(),
                mLinePaint);

        // 右下|
        canvas.drawLine(l + rectF.width(), (5 * rectF.height() / 6) + t, l
                + rectF.width(), t + rectF.height(), mLinePaint);
        // 右上|
        canvas.drawLine(l + rectF.width(), t, l + rectF.width(),
                t + (rectF.height() / 6), mLinePaint);

        // 左下-
        canvas.drawLine(l, t + rectF.height(), l + (rectF.width() / 6), t
                + rectF.height(), mLinePaint);
        // 右下-
        canvas.drawLine(l + (5 * rectF.width() / 6), t + rectF.height(), l
                + rectF.width(), t + rectF.height(), mLinePaint);
        canvas.restore();

        super.onDraw(canvas);

    }

    private void initPaint() {
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setColor(0xff00FFFF);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(3f);
        mLinePaint.setAlpha(180);
    }
}
